package uit.nfcmenu.manager;

import uit.nfcmenu.db.adapter.ItemAdapter;
import uit.nfcmenu.db.adapter.MenuAdapter;
import uit.nfcmenu.db.adapter.OrderAdapter;
import uit.nfcmenu.db.model.IDataChangeHandler;
import uit.nfcmenu.util.GetOrdersAsyncTask;
import android.content.Context;
import android.util.Log;


public class DatabaseManager {
	public static final String tag = DatabaseManager.class.getSimpleName();

	// ===========================================
	// Fields
	// ===========================================
	protected static IDataChangeHandler mMainDataScreen;

	protected Context mContext;
	protected static GetOrdersAsyncTask mGetDataAsyncTask;

	protected static DatabaseManager mInstance;
//	protected DatabaseAdapter mDatabaseAdapter;
//	protected MenuAdapter mMenuAdapter;
//	protected ItemAdapter mItemAdapter;
//	protected OrderAdapter mOrderAdapter;

	protected static int mID = 0;

	// ===========================================
	// Constructors, System Methods, Event Methods
	// ===========================================
	public DatabaseManager(Context context) {
		mContext = context;
		// mGetDataAsyncTask =
		// GetOrdersAsyncTask.getInstance(DatabaseManager.this, mContext);

//		mDatabaseAdapter = new DatabaseAdapter(mContext);
//		mMenuAdapter = new MenuAdapter(mContext);
//		mItemAdapter = new ItemAdapter(mContext);
//		mOrderAdapter = new OrderAdapter(mContext);
	}

	public static void deleteAll(Context context){
		new MenuAdapter(context).deleteDatabase();
		new ItemAdapter(context).deleteDatabase();
		new OrderAdapter(context).deleteAllOrders();
	}
	
	public static DatabaseManager getInstance(IDataChangeHandler context) {
		mMainDataScreen = context;
		if (mInstance == null) {
			mInstance = new DatabaseManager(mMainDataScreen.getContext());
			mID = 0;
		}
		return mInstance;
	}

	public void notifyDataChanged() {
		Log.i(tag, "notifyDataChanged()");
		mMainDataScreen.notifyDataChanged();

	}

	// -------------------------------------------
	// Controls Getting Data in background
	// -------------------------------------------

	public boolean start() {
		Log.i(tag, "start() // start get data from databaseManager");

		mGetDataAsyncTask = GetOrdersAsyncTask.getInstance(mMainDataScreen);
		if (!mGetDataAsyncTask.isRunning()) {
			try {
				Log.i(tag, "mGetDataAsyncTask.execute()");
				mGetDataAsyncTask.execute("");
			} catch (Exception e) {
				mGetDataAsyncTask = null;
				return false;
			}
		}
		return true;
	}

	public void stop() {
		if (mGetDataAsyncTask == null) {
			return;
		}
		mGetDataAsyncTask.stop();
		mGetDataAsyncTask = null;
	}



}
